package com.bo.entity;

import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import android.content.res.XmlResourceParser;
import android.util.Xml;

import com.bo.util.LoadResourceProxy;

public class Comment {
	

	private String productId;
	private double rate;
	private String content;
	private String time;
	

	public Comment() {
		super();
	}

	public Comment(double rate, String content ,String productId) {
		super();
		this.productId = productId;
		this.rate=rate;
		this.content = content;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}


	public double getRate() {
		return rate;
	}

	public void setRate(double rate) {
		this.rate = rate;
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public String getTime() {
		return time;
	}

	public void setTime(String time) {
		this.time = time;
	}
	
	

	  public List<Comment> createCommentList(String productId) throws Exception{
			
	    	List<Comment> list=new ArrayList<Comment>();
	    	
			String xmlStr=LoadResourceProxy.getXMLString(LoadResourceProxy.PRODUCT_COMMENT,productId);
	    	
			XmlPullParser xrp=Xml.newPullParser();

			if(xmlStr==null){
				
				throw new Exception("appear error");
			}
			
			StringReader reader=new StringReader(xmlStr);
			
			xrp.setInput(reader);
			
			  boolean isCompany=false;
			  Comment comment=null;
				
			  while(xrp.getEventType()!=XmlResourceParser.END_DOCUMENT){
						
				 if(xrp.getEventType()==XmlResourceParser.START_TAG){
							
				       String tagName=xrp.getName();
				       
					   if(tagName.equalsIgnoreCase("comment")){
								
						   isCompany=true;
						   comment=new Comment();
						       
					   }else if(tagName.equalsIgnoreCase("rate")&&isCompany){
						   
						   comment.setRate(Double.valueOf(xrp.nextText()));
						   
					   }else if(tagName.equalsIgnoreCase("content")&&isCompany){
						   	
						   comment.setContent(xrp.nextText());
						
					   }else if(tagName.equalsIgnoreCase("time")&&isCompany){
						   	
						   comment.setTime(xrp.nextText());
					   
					   }
							
				   }else if(xrp.getEventType()==XmlResourceParser.END_TAG){
					   
					   String tagName=xrp.getName();
					   
					   if(tagName.equalsIgnoreCase("comment")){
							
						   isCompany=false;    
						   
						   list.add(comment);
					   }
				   }
						
					xrp.next();
				}
			
			return list;	
	    }
}
